package com.douwong.student.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.enums.E_SubjectType;
import com.douwong.student.popuwindow.ImageShowPopu;
import com.douwong.student.popuwindow.ShowQADetailPopuWindow;
import com.douwong.student.utils.ImagFilterUtil;
import com.douwong.student.utils.LogUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 答题记录->问答互动
 * 
 * @author Tab.lv
 *
 */
public class QuizShowActivity extends BaseActivity {

	List<String> strings=new ArrayList<String>();
	private static final String TAG = QuizShowActivity.class.getSimpleName();
	private TextView tv_question, tv_Topic_type, tv_lastAnswer, tv_Correct_rate, tv_tip_title;
	private TextView tv_Judgment_right, tv_Judgment_wrong;
	private LinearLayout ll_Judgment_questions, ll_tip_box;
	private ImageView iv_answer_show_pic, iv_people;
	private ImageButton ib_submitButton;
	private String MTAG = "QuizShowActivity";
	private String TitleName, FilePath, OptionStr, StudentAnswer, TrueAnswer, CorrectRate, Type;
	int AwardCount;
	private TextView tv_star_mid_left, tv_star_mid, tv_star_mid_right, tv_star_right;
	private String flower_Str;// last start count
	private RelativeLayout rl_little;
	private TextView tv_star_mid_left_hid, tv_star_mid_hid, tv_star_mid_right_hid, tv_star_right_hid;
	// private int ivPeopleRecord = 0;
	private ScrollView sv_options;
	private LinearLayout result_ll,title_ll;
	private String[] chart = { "A", "B", "C", "D", "E", "F", "G", "H" };
	private int[] linearlayouts = new int[] { R.id.ll_A, R.id.ll_B, R.id.ll_C, R.id.ll_D, R.id.ll_E, R.id.ll_F,
			R.id.ll_G, R.id.ll_H };
	private int[] textViews = new int[] { R.id.tv_choose_A, R.id.tv_choose_B, R.id.tv_choose_c, R.id.tv_choose_D,
			R.id.tv_choose_e, R.id.tv_choose_F, R.id.tv_choose_g, R.id.tv_choose_H };
	private int width = 0, height = 0;
	long firClick;
	long secClick;
	private int Topic_type;
	private View answerquestions_content=null;
	private E_SubjectType mE_subjectType;
	@Override
	protected void initView() {
		setContentView(R.layout.answerquestions);
		answerquestions_content=findViewById(R.id.answerquestions_content);
		answerquestions_content.setVisibility(View.VISIBLE);
		answerquestions_content.setBackgroundResource(R.mipmap.answerbg);
		initViews();
		Point pt = new Point();
		getWindowManager().getDefaultDisplay().getSize(pt);
		width = pt.x;
		height = pt.y;
	}

	public void initViews() {
		findViewById(R.id.upload_ll).setVisibility(View.GONE);
		title_ll=(LinearLayout) findViewById(R.id.title_ll);
		result_ll = (LinearLayout) findViewById(R.id.result_ll);
		sv_options = (ScrollView) findViewById(R.id.sv_options);
		rl_little = (RelativeLayout) findViewById(R.id.rl_little);
		tv_star_mid_left = (TextView) findViewById(R.id.tv_star_mid_left);
		tv_star_mid = (TextView) findViewById(R.id.tv_star_mid);
		tv_star_mid_right = (TextView) findViewById(R.id.tv_star_mid_right);
		tv_star_right = (TextView) findViewById(R.id.tv_star_right);
		ll_tip_box = (LinearLayout) findViewById(R.id.tip_box);
		tv_star_mid_left_hid = (TextView) findViewById(R.id.tv_star_mid_left_hid);
		tv_star_mid_hid = (TextView) findViewById(R.id.tv_star_mid_hid);
		tv_star_mid_right_hid = (TextView) findViewById(R.id.tv_star_mid_right_hid);
		tv_star_right_hid = (TextView) findViewById(R.id.tv_star_right_hid);
		ll_Judgment_questions = (LinearLayout) findViewById(R.id.ll_Judgment_questions);
		tv_question = (TextView) findViewById(R.id.tv_question);
		tv_Judgment_right = (TextView) findViewById(R.id.tv_Judgment_right);
		tv_Judgment_wrong = (TextView) findViewById(R.id.tv_Judgment_wrong);
		tv_tip_title = (TextView) findViewById(R.id.tv_tip_title);

		tv_lastAnswer = (TextView) findViewById(R.id.tv_lastAnswer);
		tv_Correct_rate = (TextView) findViewById(R.id.tv_Correct_rate);

		ib_submitButton = (ImageButton) findViewById(R.id.ib_submitButton);
		tv_Topic_type = (TextView) findViewById(R.id.tv_Topic_type);
		iv_answer_show_pic = (ImageView) findViewById(R.id.iv_answer_show_pic);
		iv_people = (ImageView) findViewById(R.id.iv_people);
		ib_submitButton.setVisibility(View.INVISIBLE);
		Intent intent = getIntent();
		TitleName = intent.getStringExtra("TitleName");
		FilePath = intent.getStringExtra("FilePath");
		OptionStr = intent.getStringExtra("OptionStr");
		LogUtils.i(TAG,"互动问答的数据   :" +OptionStr);
		StudentAnswer = intent.getStringExtra("StudentAnswer");
		TrueAnswer = intent.getStringExtra("TrueAnswer");
		AwardCount = intent.getIntExtra("AwardCount", 0);
		CorrectRate = intent.getStringExtra("CorrectRate");
		flower_Str = String.valueOf(AwardCount);
		// StarJudgment();
		Type = intent.getStringExtra("Type");
		title_ll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				strings.clear();
				String[] split = OptionStr.split("###");

				if (split!=null&& split.length>0){
					for (int i = 0; i < split.length; i++) {
						strings.add(split[i]);
					}
				}
				ShowQADetailPopuWindow showQADetailPopuWindow=new ShowQADetailPopuWindow(QuizShowActivity.this,mE_subjectType,TitleName,strings,FilePath);
				//ShowAnswerDetailPopuWindow popu = new ShowAnswerDetailPopuWindow(QuizShowActivity.this, Topic_type + "",
				//		"result", OptionStr, TitleName);
				LogUtils.i(TAG," 问答记录   +++ 问答互动 ： "+OptionStr);
				showQADetailPopuWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
			}
		});
		if (Type.equals("单选")) {
			Topic_type=1;
			mE_subjectType=E_SubjectType.single;
			showTextView(OptionStr);
			tv_Topic_type.setBackgroundResource(R.mipmap.radiobuttonlistpic);
			RadioTreatment();
		} else if (Type.equals("多选")) {
			Topic_type=2;
			mE_subjectType=E_SubjectType.multiple;
			showTextView(OptionStr);
			tv_Topic_type.setBackgroundResource(R.mipmap.multiple_choice_question_pic);
			MultipleProcessing();
		} else if (Type.equals("判断")) {
			Topic_type=0;
			mE_subjectType=E_SubjectType.judgment;
			for (int i = 0; i < chart.length; i++) {
				findViewById(linearlayouts[i]).setVisibility(View.GONE);
			}
			ll_Judgment_questions.setVisibility(View.VISIBLE);
			tv_Topic_type.setBackgroundResource(R.mipmap.judgment_questions_pic);
			tv_Judgment_right.setClickable(false);
			tv_Judgment_wrong.setClickable(false);

			JudgmentProcessing();
		}

		result_ll.setVisibility(View.VISIBLE);
		tv_lastAnswer.setText("正确答案:" + TrueAnswer);
		tv_Correct_rate.setText("正确率:" + CorrectRate);
		//设置标题
	//	tv_question.setText(TitleName);
		ImagFilterUtil.filterImage(tv_question,TitleName,1);

		if (FilePath == null) {
			iv_answer_show_pic.setVisibility(View.GONE);
		} else {
			iv_answer_show_pic.setVisibility(View.VISIBLE);
			final File file = new File(FilePath);
			if (file.exists()) {
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 2;//图片宽高都为原来的二分之一，即图片为原来的四分之一
				if(file.length()>200*1024){
					attachFileBitmap=BitmapFactory.decodeFile(FilePath,options);
				}else{
					attachFileBitmap = BitmapFactory.decodeFile(FilePath);
				}
				if(attachFileBitmap!=null){
					iv_answer_show_pic.setImageBitmap(attachFileBitmap);
				}
			}
			iv_answer_show_pic.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (attachFileBitmap != null) {
						showAnswerPic(attachFileBitmap);
					}

				}
			});
		}
	}

	private Bitmap attachFileBitmap = null;
	private Dialog answerPicDialog = null;

	// 显示答题图片
	public void showAnswerPic(Bitmap bitmap) {
		if (bitmap != null) {
			ImageShowPopu popu = new ImageShowPopu(this, bitmap, width, height);
			popu.showAtLocation(iv_answer_show_pic, Gravity.CENTER, 0, 0);
		}

	}
	/**
	 * 显示控件
	 * @param OptionStr
     */
	public void showTextView(String OptionStr) {
		String Str[] = OptionStr.split("###");


		//给每个选项赋值
		for (int i = 0; i < Str.length; i++) {
			findViewById(linearlayouts[i]).setVisibility(View.VISIBLE);
			TextView text = (TextView) findViewById(textViews[i]);
					String selectValue=Str[i];
				//	text.setText(selectValue.substring(2,selectValue.length()-1));

			LogUtils.i(TAG," l问答互动 选项答案 ：" +selectValue);
			ImagFilterUtil.filterImage(text,selectValue.substring(2,selectValue.length()),1);
		}

	}

	// 判断处理
	public void JudgmentProcessing() {

		if (!StudentAnswer.equals("")) {
			if (StudentAnswer.equals(TrueAnswer)) {

				iv_people.setBackgroundResource(R.mipmap.answerright);
			} else {

				iv_people.setBackgroundResource(R.mipmap.answerwrong_people);
			}
			if (StudentAnswer.equalsIgnoreCase("Yes")){
				tv_Judgment_right.setBackgroundResource(R.mipmap.answer_right_r);
			}else if (StudentAnswer.equalsIgnoreCase("No")){
				tv_Judgment_wrong.setBackgroundResource(R.mipmap.answer_wrong_r);
			}

			tv_tip_title.setBackgroundResource(R.mipmap.prompt_w);
		}else{
			iv_people.setBackgroundResource(R.mipmap.answerwrong_people);
		}

	}

	private void showResult(String str, int id) {
		for (int i = 0; i < chart.length; i++) {
			if (str.equals(chart[i])) {
				findViewById(linearlayouts[i]).setBackgroundResource(id);
				break;
			}
		}
	}

	// 单选处理
	public void RadioTreatment() {
		iv_people.setBackgroundResource(R.mipmap.answerright);
		tv_tip_title.setBackgroundResource(R.mipmap.prompt_r);
		Log.i(MTAG, "singleChoice result : " + StudentAnswer);
		showResult(StudentAnswer, R.mipmap.option_box_r);
		if (!StudentAnswer.equals(TrueAnswer)) {
			tv_tip_title.setBackgroundResource(R.mipmap.prompt_w);
			iv_people.setBackgroundResource(R.mipmap.answerwrong_people);
		}
	}

	// 多选处理
	public void MultipleProcessing() {
		tv_tip_title.setBackgroundResource(R.mipmap.prompt_r);
		iv_people.setBackgroundResource(R.mipmap.answerright);
		if (StudentAnswer.length()>0){
			char[] StrStudent=	StudentAnswer.toCharArray();
			for (int i = 0; i < StrStudent.length; i++) {
				String str=String.valueOf(StrStudent[i]);
				showResult(str, R.mipmap.option_box_r);
			}
		}


		if (!StudentAnswer.equals(TrueAnswer)) {
			tv_tip_title.setBackgroundResource(R.mipmap.prompt_w);
			iv_people.setBackgroundResource(R.mipmap.answerwrong_people);
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		System.gc();
		try {
			if (answerPicDialog != null && answerPicDialog.isShowing()) {
				answerPicDialog.dismiss();
			}
			if (attachFileBitmap != null) {
				attachFileBitmap.recycle();
				attachFileBitmap=null;
			}
			//ViewGroup 背景 子类会抛异常
			BitmapDrawable bitmap=(BitmapDrawable)answerquestions_content.getBackground();
			answerquestions_content.setBackgroundResource(0);
			if(bitmap!=null){
				bitmap.setCallback(null);
//				bitmap.getBitmap().recycle();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
	  }		
	}


}
